const express = require('express');
const router = express.Router();
const db = require('../db/database');

// 学生登录
router.post('/student/login', (req, res) => {
  // TODO: 实现学生登录逻辑
  res.json({ success: true });
});

// 提交答案
router.post('/student/answer/submit', express.json(), (req, res) => {
  const { studentName, questionId, answerText } = req.body;
  
  db.get('SELECT answer FROM questions WHERE id = ?', [questionId], (err, row) => {
    if (err) return res.status(500).json({ error: '获取题目失败' });
    
    const isCorrect = row.answer === answerText;
    
    db.run(
      'INSERT INTO answers (student_name, question_id, answer_text, is_correct) VALUES (?, ?, ?, ?)',
      [studentName, questionId, answerText, isCorrect ? 1 : 0],
      (err) => {
        if (err) return res.status(500).json({ error: '提交失败' });
        res.json({ 
          success: true,
          isCorrect,
          correctAnswer: row.answer
        });
      }
    );
  });
});

module.exports = router;